package be.lreenaers.kaktus.client;




import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Kaktus implements EntryPoint{
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";
	final private PopupPanel popupPanel = new PopupPanel(true);
	Command showAlertCommand = new Command() {
		public void execute() {
			popupPanel.hide();
			Window.alert("Hope this example helps.");
		}
	};
	private DockPanel dock = new DockPanel();
	
	private Tree menu = createStaticTree();
	private PathWay pathway = new PathWay(menu);
	private HorizontalPanel pathpanel;
	private HTML centerPg = new HTML("center<br/>blahblalkgfsdklfjgfskldjglskdfjgskljglksfjgdlfkjgldkjfgldkfjgldfkjgldfkjggjldkjgldkfjgldfkjgldfkjg");
	final MenuBar contextMenuBar = new MenuBar(true);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		menu.setAnimationEnabled(true);
		menu.ensureDebugId("cwTree-staticTree");
		VerticalPanel staticTreeWrapper = new VerticalPanel();
		staticTreeWrapper.add(menu);
	    staticTreeWrapper.ensureDebugId("cwTree-staticTree-Wrapper");
	    //staticTreeWrapper.setSize("300px", "300px");

	    // Wrap the static tree in a DecoratorPanel
	    DecoratorPanel staticDecorator = new DecoratorPanel();
	    staticDecorator.setWidget(staticTreeWrapper);
		this.pathpanel = pathway.getPanel();
		//createPopupMenu();
		dock.setStyleName("kakt-DockPanel");
	    dock.setSpacing(4);
	    dock.setHorizontalAlignment(DockPanel.ALIGN_CENTER);
	    dock.add(new HTML("<h1>Kaktus Administration</h1>"), DockPanel.NORTH);
	    dock.add(new HTML("Footer power by <a href=''>KAktus</a>"), DockPanel.SOUTH);
	    dock.add(new HTML("east"), DockPanel.EAST);
	    dock.add(staticDecorator, DockPanel.WEST);
	    //dock.add(new HTML("<a href=''>HOME</a> > <a href=''>TOTO</a> > <a href=''>TOTO2</a> > <a href=''>BLABLA</a> > <a href=''>PATHWAY</a>"), DockPanel.NORTH);
	    dock.add(pathpanel,DockPanel.NORTH);
	    //dock.add(new HTML("south2"), DockPanel.SOUTH);
	    dock.add(centerPg, DockPanel.CENTER); 
	    dock.ensureDebugId("kakDocPanel");
	    
	    RootPanel.get("kaktusadm").add(dock);
	}
	private void updateUI(ItemInfo inf, TreeItem itm){
		centerPg.setHTML("contenttype:"+inf.getType()+"<br/>content:"+itm.getText());
		dock.remove(pathpanel);
		pathpanel = pathway.getPanel();
		dock.add(pathpanel,DockPanel.NORTH);
		
		Window.alert("update ui");
	}
	private Tree createStaticTree() {
	    // Create the tree
	    String[] composers = {"compo1","compo1"};
	    String concertosLabel = "concerto4";
	    
	    Tree staticTree = new Tree();
	    staticTree.addMouseUpHandler(new MouseUpHandler() {
			
			@Override
			public void onMouseUp(MouseUpEvent event) {
				if(event.getNativeButton()== Event.BUTTON_LEFT){
				Tree tree = (Tree)event.getSource();
				TreeItem itm = tree.getSelectedItem();
				ItemInfo inf = (ItemInfo) itm.getUserObject();
				updateUI(inf, itm);
				}
			}
		});
	    // Add some of Beethoven's music
	    TreeItem beethovenItem = staticTree.addItem(composers[0]);
	    ItemInfo inf2 = new ItemInfo("cat");
	    beethovenItem.setUserObject(inf2);
	    TreeItem section = beethovenItem.addItem(concertosLabel);
	    section.setUserObject(inf2);
	    
	    for (String work : composers) {
	    	//Anchor anch =new Anchor(work);
	    	Anchor anch = new Anchor(work);
	    	anch.setHref("http://www.google.com");
	        TreeItem itm = section.addItem(work);
	        ItemInfo inf = new ItemInfo("doc");
	        itm.setUserObject(inf);
	      }
	    beethovenItem.setState(true);
	    section.setState(true);
	    staticTree.setSelectedItem(beethovenItem);
	    // Return the tree
	    return staticTree;
	  }



//	@Override
//	public void onRightClick(Widget sender, Event event) {
//		int x = DOM.eventGetClientX(event);
//    	int y = DOM.eventGetClientY(event);
//    	popupPanel.setPopupPosition(x, y);
//    	popupPanel.show();
//		
//	}



	
	
}
